package com.base.util.javautils.observer;

import java.util.Map;
import java.util.Objects;
import java.util.WeakHashMap;

/**
 * Created by gj on 16-1-24.
 * 必须在主线程中初始化
 */
public class ObserverFectory {
    
    private static final Map<Object, IObservable> MAP_OBJECT_OBSERVABLE = new WeakHashMap<>();

    public static synchronized IObservable createObservable(Object Observed, boolean isMainThreadObservable) {
        IObservable observable = MAP_OBJECT_OBSERVABLE.get(Observed);
        if (observable == null) {
            if (isMainThreadObservable) {
                observable = new ObservableForMainThread();
            } else {
                observable = new ObservableForThread();
            }
            MAP_OBJECT_OBSERVABLE.put(Observed, observable);
        }
        return observable;
    }

    public static synchronized IObservable getObservableController(Object Observed) {
        return MAP_OBJECT_OBSERVABLE.get(Observed);
    }

}
